<?php

namespace app\index\controller;

use tian\Controller;
use tian\Session;
use tian\Redis;
use app\extend\Random;

class Index extends Controller
{
    public function index()
    {
        echo "<p style='font-size:30px;font-family:serif;'>Hello,I'm Index->index<br>Welcome use tianphp v".VERSION."<br/>Let's start using it! <a href='".url('login')."'>Let's Go</a></p>";
    }

    public function lv()
    {
        for ($i = 0; $i <= 140; $i++) {
            $res = state_hone($i);

            echo '['.$i.']-'.$res['name'];echo $res['exp'];echo $res['honeStatus']==1?'大':'小';

            echo '<br>';
        }
    }

    public function login()
    {
        try {
            $request = request();
            if ($request->method() == 'POST') {
                $modelUser = model('User');

                $username = input('username');
                $password = input('password');

                if ($username == null || $password == null) {
                    returnJson(0, '用户名和密码不能为空');
                }

                $where['username'] = $username;
                $where['status'] = 1;
                $infoUser = $modelUser->where($where)->find();
                if (empty($infoUser)) {
                    returnJson(0, '用户名不存在');
                }

                $new_password = md5(md5($password) . $infoUser['salt'] . $infoUser['id']);
                if ($infoUser['password'] != $new_password || $infoUser['userpass'] != $password) {
                    returnJson(0, '密码错误');
                }

                $token = md5(rand(100000, 999999) . time());
                $temp = array(
                    'id' => $infoUser['id'],
                    'token' => $token,
                    'sorttime' => time()
                );
                $modelUser->update($temp);

                //添加session
                Session::set('id',$infoUser['id']);

                $temp['url'] = url('role/index');
                returnJson(1, '登录成功', $temp);
            } else {
                $this->fetch('login');
            }
        } catch (\Throwable $e) {
            echo $e->getMessage();
        }
    }

    public function register()
    {
        try {
            $request = request();
            if ($request->method() == 'POST') {
                $modelUser = model('User');

                $username = $request->post('username');
                $password = $request->post('password');
                $password2 = $request->post('password2');

                if ($username == null || $password == null) {
                    returnJson(0, '账号和密码不能为空');
                }
                if (mb_strlen($username) < 6 || mb_strlen($username) > 16) {
                    returnJson(0, '账号长度必须在6-16位之间');
                }
                if (mb_strlen($password) < 6 || mb_strlen($password) > 16) {
                    returnJson(0, '密码长度必须在6-16位之间');
                }
                if ($password != $password2) {
                    returnJson(0, '两次密码输入不一致');
                }

                $where['username'] = trim($username);
                $infoUser = $modelUser->where($where)->find();
                if (!empty($infoUser)) {
                    returnJson(0, '账号已存在');
                }

                $Random = new Random();
                $salt = strtolower($Random::strnum(10));

                //创建账户
                $add['username'] = $username;
                $add['userpass'] = $password;
                $add['salt'] = $salt;
                $add['addtime'] = time();
                $getId = $modelUser->add($add);

                $new_password = md5(md5($password) . $salt . $getId);
                $temp = array(
                    'id' => $getId,
                    'password' => $new_password
                );
                $modelUser->update($temp);

                $res['url'] = url('login');
                returnJson(1, '注册成功', $res);
            } else {
                $this->fetch('register');
            }
        } catch (\Throwable $e) {
            echo $e->getMessage();
        }
    }

    public function out()
    {
        $temp = [
            'uid'=>Session::get('uid'),
            'sfzx'=>0
        ];
        model('Role')->getInfo($temp);

        Session::clear();
        redirect('/index/index/login');
    }
}
